package demo

import (
	"context"

	"bitwormhole.com/starter/cli"
	"github.com/bitwormhole/starter/application"
	"github.com/bitwormhole/starter/markup"
)

// TestPoint ...
type TestPoint struct {
	markup.Component `class:"life"`

	CLI cli.CLI `inject:"#cli"`
	WD  string  `inject:"${test.wd}"`
}

func (inst *TestPoint) _Impl() application.LifeRegistry {
	return inst
}

// GetLifeRegistration ...
func (inst *TestPoint) GetLifeRegistration() *application.LifeRegistration {
	return &application.LifeRegistration{
		OnStart: inst.start,
	}
}

func (inst *TestPoint) start() error {

	cmds := []string{
		"git-add .",
		"git-commit -m 'demo'",
	}

	ctx := context.Background()
	ctx = inst.CLI.Bind(ctx)
	client := inst.CLI.GetClient()

	t := &cli.Task{
		Context: ctx,
		WD:      inst.WD,
	}

	for _, cmd := range cmds {
		t.Command = cmd
		t.Arguments = nil
		err := client.Run(t)
		if err != nil {
			return err
		}
	}

	return nil
}
